<?php
namespace Nursing\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class NursingController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');	
		
    }
    public function get($userId)
    {			
		header('Access-Control-Allow-Origin: *');
		$values=explode('-',$userId);
		$user_id=$values[0];
		$type=$values[1];
		if($type==0){
			$requestServiceTestsTable=$this->getServiceLocator()->get('Models\Model\RequestServiceTestsFactory');
			$requestServiceTests = $requestServiceTestsTable->getAllTests()->toarray();
			$insurancesTable=$this->getServiceLocator()->get('Models\Model\InsurancesFactory');
			$insurances = $insurancesTable->getAllInsurances()->toarray();
			$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
			$getuserData = $userDetailsTable->getuserdetailesData($user_id)->toArray();
			if(count($requestServiceTests)!=0){
				return new JsonModel(array(
					'data' 				=> json_encode($requestServiceTests),
					'insurances' 		=> json_encode($insurances),
					'userdata' 		    => json_encode($getuserData),
					'value' 			=> 1,
					'output' 			=> 'success',
				));
			}else{
				return new JsonModel(array(
					'output' 	=> 'fail',
					'value' 	=> 0,
				));	
			}
		}else{
			$requestServicesTable=$this->getServiceLocator()->get('Models\Model\RequestServicesFactory');
			$requestServices = $requestServicesTable->getTests($user_id)->toarray();
			if(count($requestServices)!=0){
				return new JsonModel(array(
					'result' 		=> json_encode($requestServices),
					'value' 	    => 1,
					'output' 		=> 'success',
				));
			}else{
				return new JsonModel(array(
					'output' 	=> 'fail',
					'value' 	=> 0,
				));	
			}
		}
    }
    public function create($tests)
    {
		header('Access-Control-Allow-Origin: *');
		$requestServicesTable=$this->getServiceLocator()->get('Models\Model\RequestServicesFactory');
		$requestServices = $requestServicesTable->addTests($tests);
		if($requestServices){
			return new JsonModel(array(
				'data' 		=> $requestServices,
				'value' 	=> 1,
				'output' 	=> 'success',
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'fail',
				'value' 	=> 0,
			));	
		}
    }
    public function update($uid,$passwords)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$check_old_password = $userTable->getpassword($uid,$passwords['old_password'])->toarray();
		if(count($check_old_password)!=0){
			$change_password = $userTable->changepwd($uid,$passwords['cnf_password']);
			if($change_password==0 || $change_password==1){
				return new JsonModel(array(
					'value' 	=> 1,
					'output' 	=> 'succe1',
				));
			}
		}else{
			return new JsonModel(array(
				'output' 	=> 'fail',
				'value' 	=> '0',
			));	
		}
    }
    public function delete($id)
    {
       
    }
}